package com.imcode;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SerializableDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Teacher t1 = new Teacher("张三");
        Teacher t2 = new Teacher("李四");
        Teacher t3 = new Teacher("王五");

        // 将三个老师的数据序列化到文件中
        //1.将老师的信息放入集合中
        List<Teacher> list = new ArrayList<>();
        list.add(t1);
        list.add(t2);
        list.add(t3);
        //2.创建一个序列化流
        ObjectOutputStream out =
                new ObjectOutputStream(new FileOutputStream("file/list.data"));
        //3.对象序列胡
        out.writeObject(list);
        out.close();

        //4.反序列化集合对象
        ObjectInputStream in =
                new ObjectInputStream(new FileInputStream("file/list.data"));
        List<Teacher> teacherList = (List<Teacher>) in.readObject();
        for (Teacher t : teacherList) {
            System.out.println(t.getName());
        }
        in.close();
    }
}
